package com.ginage.gaugemanager.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ginage.gaugemanager.mapper.entity.Department;
import com.ginage.gaugemanager.mapper.entity.GaugeType;
import com.ginage.gaugemanager.service.api.ICompanyService;


@Controller
public class CompanyController {
	@Autowired
	private ICompanyService service;
	
	@RequestMapping("getGaugeTypeAndDepartmentInfo.do")
	@ResponseBody
	public void getGaugeType(HttpServletResponse response) {
		List<GaugeType> gaugeTypeList=service.getGaugeType();
		List<Department> departmentList=service.getDepartment();
		JSONArray ja=new JSONArray();
		JSONObject jb=new JSONObject();
		JSONObject jb2=new JSONObject();
		for(GaugeType gt:gaugeTypeList) {
			jb.put( gt.getGaugeName(),gt.getAbbreviation());
		}
		for(Department dt:departmentList) {
			jb2.put(dt.getFactory()+"-"+dt.getDepartmentName(),dt.getDepartmentNum());
		}
		ja.add(jb);
		ja.add(jb2);
		try {
			response.getWriter().println(ja);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	@RequestMapping("getDepartmentInfo.do")
	public void getDepartment(HttpServletResponse response) {
		List<Department> departmentList=service.getDepartment();
		JSONObject jb=new JSONObject();
		for(Department dt:departmentList) {
			jb.put(dt.getId()+"", dt.getFactory()+"-"+dt.getDepartmentName());
		}
		try {
			response.setContentType("text/html; charset=utf-8");
			response.setCharacterEncoding("UTF-8");
			response.getWriter().print(jb);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	
	
	
}
